package com.ego.ware.po;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;
import java.io.Serializable;

import com.ego.common.constant.WareConstant;
import com.ego.ware.vo.PurchaseDetailVo;
import lombok.Data;

/**
 * @author KamTang
 * @program: ego
 * @description 采购需求
 * @date 2022-11-13 13:30:29
 */
@Data
@TableName("wms_purchase_detail")
public class PurchaseDetail implements Serializable {
    private static final long serialVersionUID = 1L;

    @TableId
    private Long id;
    /**
     * 采购单id
     */
    private Long purchaseId;

    /**
     * 分类id
     */
    private Long categoryId;

    /**
     * 分类路径
     */
    @TableField(exist = false)
    private Long[] categoryPath;

    /**
     * 品牌id
     */
    private Long brandId;

    /**
     * 采购商品id
     */
    private Long skuId;

    /**
     * 采购商品名称
     */
    private String skuName;

    /**
     * 采购数量
     */
    private Integer skuNum;
    /**
     * 采购金额
     */
    private BigDecimal skuPrice;

    /**
     * 仓库id
     */
    private Long wareId;

    /**
     * 仓库名称
     */
    private String wareName;

    /**
     * 状态[0新建，1已分配，2正在采购，3已完成，4采购失败]
     */
    private Integer status;

    /**
     * 是否可合并
     */
    public boolean mergeable() {
        return this.getStatus() == WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode() ||
                this.getStatus() == WareConstant.PurchaseDetailStatusEnum.CREATED.getCode();
    }

    /**
     * 采购中
     */
    public void purchasing(PurchaseDetail purchaseDetail) {
        this.id = purchaseDetail.id;
        this.status = WareConstant.PurchaseDetailStatusEnum.BUYING.getCode();
    }

    public static PurchaseDetail fromVo(PurchaseDetailVo vo) {
        return BeanUtil.copyProperties(vo, PurchaseDetail.class);
    }

    public PurchaseDetailVo toVo() {
        return BeanUtil.copyProperties(this, PurchaseDetailVo.class);
    }

    /**
     * 合并到采购单
     * @param purchaseId 采购单id
     */
    public void assign(Long purchaseId) {
        this.purchaseId = purchaseId;
        this.status = WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode();
    }
}
